package cn.lyl.org.service.impl;

import cn.lyl.basic.exception.BusinessException;
import cn.lyl.basic.service.impl.BaseServiceImpl;
import cn.lyl.basic.util.BaiduAuditUtils;
import cn.lyl.basic.util.Md5Utils;
import cn.lyl.basic.util.StrUtils;
import cn.lyl.org.domain.Employee;
import cn.lyl.org.domain.Shop;
import cn.lyl.org.domain.ShopAuditLog;
import cn.lyl.org.dto.ShopDto;
import cn.lyl.org.mapper.EmployeeMapper;
import cn.lyl.org.mapper.ShopAuditLogMapper;
import cn.lyl.org.mapper.ShopMapper;
import cn.lyl.org.service.IShopService;
import cn.lyl.user.domain.Logininfo;
import cn.lyl.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void settlement(Shop shop) {
        //一、校验
        //1.空值校验
        if(StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getAddress())){
            throw new BusinessException("信息不能为空");
        }
        //2.两次密码不一致校验
        if(!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())){
            throw new BusinessException("两次密码不一致");
        }
        //3.当前店铺是否入驻过
        Shop dbshop = shopMapper.findByNameAndAddress(shop);
        if(dbshop != null){
            throw new BusinessException("当前店铺已经入驻过，请直接登录，忘记密码请联系管理员！");
        }

        //二.百度AI只能审核【店铺名，logo是否合法】
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名不合规！！");
        }
        if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺Logo不合规！！");
        }
        //三：添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);
        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.add(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.add(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);
        /*employeeMapper.add(admin);//shop_id = null 会返回自增id3个属性
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);//会返回自增id
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId());//id,shopId
        employeeMapper.update(admin);
        //四：人工审核之后发送激活邮件*/


    }

    @Override
    public void auditReject(ShopAuditLog log) {
        //1.改状态：1待审核 - 4审核失败
        shopMapper.updateState(4,log.getShopId());
        //2.添加日志
        log.setState(0);//审核失败
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发邮件给店长通知他重新修改信息入驻
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1501828087@qq.com");
        Shop shop = shopMapper.findOne(log.getShopId());
        //邮件主题
        mailMessage.setSubject("店铺审核失败");
        //邮件内容：普通文件无法解析html标签
        mailMessage.setText("<h1>请重新填写入驻信息</h1>");
        //收件人
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        mailMessage.setTo(employee.getEmail());
        //发送邮件
        javaMailSender.send(mailMessage);



    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 4审核通过
        shopMapper.updateState(2, log.getShopId());
        //2.添加日志
        log.setState(1);//审核成功
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发邮件给店长通知他激活入驻
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1501828087@qq.com");//平台的邮箱
        helper.setSubject("店铺激活邮件");
        Shop shop = shopMapper.findOne(log.getShopId());

        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+
                shop.getLogo()+"' ><a href='http://172.16.6.174:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);
        //添加附件
        /*helper.addAttachment("zhm.jpg",new File("C:\\Users\\Mr.Li\\Desktop\\03_images\\zhm.jpg"));*/
        //收件人
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);


    }

    @Override
    public void active(Long id) {
        shopMapper.updateState(3,id);

    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
